package com.fh.modules.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fh.common.util.StringUtils;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.dbms.entity.DbmsPackage;
import com.fh.modules.dbms.entity.DbmsPackageBody;
import com.fh.modules.dbms.entity.DbmsPackageDeclare;
import com.fh.modules.dbms.entity.DbmsTableIndexColumn;
import com.fh.modules.dbms.mapper.DbmsPackageBodyMapper;
import com.fh.modules.dbms.mapper.DbmsPackageMapper;
import com.fh.modules.dbms.service.IDbmsPackageBodyService;
import com.fh.modules.dbms.service.IDbmsPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 23/07/2020
 * @Description
 */
@Service
public class DbmsPackageBodyServiceImpl extends AbstractServiceImpl<DbmsPackageBodyMapper, DbmsPackageBody> implements IDbmsPackageBodyService {

    @Autowired
    private DbmsPackageBodyMapper dbmsPackageBodyMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public int deleteByPackageId(Long packageId) {
        UpdateWrapper<DbmsPackageBody> updateWrapper=new UpdateWrapper<DbmsPackageBody>();
        updateWrapper.eq("package_id",packageId);
        return dbmsPackageBodyMapper.delete(updateWrapper);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean save(DbmsPackageBody entity) {
        return super.save(entity);
    }

    @Override
    public boolean comparePackageBody(Long packageId1,Long packageId2){
        List<DbmsPackageBody> list1=this.queryDbmsPackageBodys(packageId1);
        List<DbmsPackageBody> list2=this.queryDbmsPackageBodys(packageId2);
        if(list1.size()==list2.size()){
            int length=list1.size();
            for(int i=0;i<length;i++){
                String content1=list1.get(i).getContent();
                String content2=list2.get(i).getContent();
                if(!StringUtils.compareStr(content1,content2)){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public List<DbmsPackageBody> queryDbmsPackageBodys(Long packageId){
        QueryWrapper<DbmsPackageBody> queryWrapper = new QueryWrapper<DbmsPackageBody>();
        queryWrapper.eq("package_id", packageId).eq("del_flag",0);
        queryWrapper.orderByAsc("line_id");
        return this.list(queryWrapper);
    }
}
